<?php
include_once("module/payment/includes/payment_class.php");
include_once("module/member/includes/plugin_member_class.php");
$member = new member();
$payment=new payment();
//============================================================
$re=$payment->payment_base();
$tpl->assign("account",$re);

//----------------
$order_id=$_GET['order_id'];
//----------------当前流水详情	
$sql="select * from ".CASHFLOW." where order_id='$order_id' and pay_uid='".$payment->pay_uid."'";
$db->query($sql);
$de=$db->fetchRow();
$tpl->assign("de",$de);

//订单信息
$o_sql = "select * from ".ORDER." where order_id=".$order_id;
$db->query($o_sql);
$order_info = $db->fetchRow();

if(!empty($_GET['payment']) and is_numeric($_GET['payment']))
{
	$sql = "select payment_id,payment_type,payment_name from ".PAYMENT." where payment_id='$_GET[payment]'";
	$db->query($sql);
	$pay = $db->fetchRow();
	//换了支付方式 更改支付信息
	if($order_info['payment_id'] != $pay['payment_id']){
		$o_usql = "update ".ORDER." set payment_id = '{$pay['payment_id']}', payment_name= '{$pay['payment_name']}' where order_id = '$order_id'  ";
		$db->query($o_usql);
	}
    if($order_info['payment_id'] != $pay['payment_id'] ){
		if($order_info['payment_id'] == 9){
			msg($config['weburl']."/main.php?m=product&s=admin_order_pay&order_id=".$order_id,"很抱歉，因此订单由浏览器页面下单购买，请返回浏览器页面重新登录选择支付宝方式完成支付");
			return false;
		} elseif($order_info['payment_id'] == 8){
			msg($config['weburl']."/main.php?m=product&s=admin_order_pay&order_id=".$order_id,"很抱歉，因此订单由微信接口下单购买，请返回微信页面重新登录帐户完成支付");
			return false;
		} else {
			msg($config['weburl']."/main.php?m=product&s=admin_order_pay&order_id=".$order_id,"请点击当前支付方式支付");
			return false;
		}
	}
//	$user_agent = $_SERVER['HTTP_USER_AGENT'];
//	if($pay['payment_type'] == 'wechatpay' && strpos($user_agent, 'MicroMessenger') === false){
//		msg($config['weburl']."/main.php?m=product&s=admin_order_pay&order_id=".$order_id,"请更换支付方式");
//		return false;
//	}
	if($pay['payment_id'] and $pay['payment_type']!='account' and $pay['payment_type']!='cod')
	{

		$price=$de['price']*-1;

		header("Location: main.php?m=payment&s=admin_accounts_pay&order_id=$order_id&payment=$_GET[payment]");
		die;
	}
}
//----------------进行支付,且必须是新订单
if($_POST['submit']&&$de['statu']==1)
{
	if($re['pay_pass']==md5(trim($_POST['pay_pass']))&&!empty($_POST['pay_pass']))
	{
		//-------------减少账户金额
		if($de['price']<0) $de['price']*=-1;
		if($re['cash']<$de['price'])
		{
			$price=$de['price']-$re['cash'];
			$admin->msg("main.php?m=payment&s=admin_accounts_pay&price=$price",'支付金额不足','failure');
		}
		else
		{
			$sql = "update ".PUSER." set cash=cash-".$de['price'].",unreachable=unreachable+".$de['price']." where pay_uid='$re[pay_uid]'";
			$db->query($sql);

			include_once("module/product/includes/plugin_order_class.php");
			$order = new order();		
			$order->set_order_statu($order_id,2);
			
			//--异步处理,以后处理
			
			//返回同步处理结果
			$url=$de['return_url']."&order_id=$order_id&price=".($de['price']*-1)."&extra_param=$de[extra_param]&statu=1&auth=".md5($config['authkey']);
			msg($url);
		}
	}
	else
	{
		$admin->msg("main.php?m=payment&s=admin_pay&order_id=$order_id",'支付密码错误','failure');
	}
}
//============================================================
$tpl->assign("config",$config);
$tpl->assign("lang",$lang);
$output=tplfetch("admin_pay.htm");
?>